#include "muduo/proto/codec.h"
#include "muduo/proto/dispatcher.h"

#include "request.pb.h" // proto
#include "muduo/base/Logging.h"
#include "muduo/base/Mutex.h"
#include "muduo/net/EventLoop.h"
#include "muduo/net/TcpServer.h"

#include <iostream>
#include <unordered_map>

class Server
{
public:
    typedef std::shared_ptr<google::protobuf::Message> MessagePtr;
    typedef std::shared_ptr<Resq::AddRequest> AddRequestPtr;
    typedef std::shared_ptr<Resq::TranslateRequest> TranslateRequestPtr;

public:
    Server(int port)
        : _server(&_baseloop, muduo::net::InetAddress("0.0.0.0", port), "Server", muduo::net::TcpServer::kNoReusePort),
          _dispatcher(std::bind(&Server::UnKnownMessage, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)),
          _codec(std::bind(&ProtobufDispatcher::onProtobufMessage, &_dispatcher, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3))
    {
        _dispatcher.registerMessageCallback<Resq::AddRequest>(std::bind(&Server::onAdd, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
        _dispatcher.registerMessageCallback<Resq::TranslateRequest>(std::bind(&Server::onTranlate, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));

        _server.setConnectionCallback(std::bind(&Server::onConnection, this, std::placeholders::_1));
        _server.setMessageCallback(std::bind(&ProtobufCodec::onMessage, &_codec, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
    }

    void start()
    {
        _server.start();
        _baseloop.loop();
    }

private:
    std::string translate(const std::string &str)
    {
        static std::unordered_map<std::string, std::string> dict_map = {
            {"hello", "你好"},
            {"Hello", "你好"},
            {"你好", "Hello"},
            {"吃了吗", "油泼面"}};
        auto it = dict_map.find(str);
        if (it == dict_map.end())
        {
            return "没听懂！！";
        }
        return it->second;
    }
    void UnKnownMessage(const muduo::net::TcpConnectionPtr &conn, const MessagePtr &message, muduo::Timestamp)
    {
        LOG_INFO << "onUnknownMessage: " << message->GetTypeName();
        conn->shutdown();
    }
    void onTranlate(const muduo::net::TcpConnectionPtr &conn, const TranslateRequestPtr &message, muduo::Timestamp)
    {
        // 获取message中的有效载荷
        std::string str = message->mes();
        // 翻译得到结果
        std::string res = translate(str);
        // 构建报文
        Resq::TranslateResponse resp;
        resp.set_mes(res);
        // 发送响应
        _codec.send(conn, resp);
    }
    void onAdd(const muduo::net::TcpConnectionPtr &conn, const AddRequestPtr &message, muduo::Timestamp)
    {
        int num1 = message->num1();
        int num2 = message->num2();

        int result = num1 + num2;
        Resq::AddResponse resq;
        resq.set_result(result);

        _codec.send(conn, resq);
    }
    void onConnection(const muduo::net::TcpConnectionPtr &conn)
    {
        if (conn->connected())
        {
            std::cout << "新连接建立成功！" << std::endl;
        }
        else
        {
            std::cout << "即将断开连接！" << std::endl;
        }
    }

private:
    muduo::net::TcpServer _server;
    muduo::net::EventLoop _baseloop; // 事件循环
    ProtobufDispatcher _dispatcher;  // 分发器
    ProtobufCodec _codec;            // 协议处理
};

int main()
{
    Server server(8888);
    server.start();
    return 0;
}